FROM openeuler-22.03-lts-sp4:latest AS build

ARG RESTY_VERSION=1.27.1.2
ARG ZLIB_VERSION=1.3.1
ARG PCRE2_VERSION=10.44
ARG OPENSSL_VERSION=3.4.1

ARG RESTY_PREFIX="/usr/local/openresty"
ARG ZLIB_PREFIX="${RESTY_PREFIX}/zlib"
ARG PCRE_PREFIX="${RESTY_PREFIX}/pcre2"
ARG OPENSSL_PREFIX="${RESTY_PREFIX}/openssl3"

ARG ZLIB_BUILD_OPTS="--enable-shared"
ARG PCRE_BUILD_OPTS="--enable-jit --enable-pcre2grep-jit --disable-bsr-anycrlf --disable-coverage --disable-ebcdic --disable-fuzz-support \
    --disable-jit-sealloc --disable-never-backslash-C --enable-newline-is-lf --enable-pcre2-8 --enable-pcre2-16 --enable-pcre2-32 \
    --enable-pcre2grep-callout --enable-pcre2grep-callout-fork --disable-pcre2grep-libbz2 --disable-pcre2grep-libz --disable-pcre2test-libedit \
    --enable-percent-zt --disable-rebuild-chartables --enable-shared --disable-static --disable-silent-rules --enable-unicode --disable-valgrind \
    "
ARG OPENSSL_BUILD_OPTS="shared enable-camellia enable-seed enable-rfc3779 enable-cms enable-md2 enable-rc5 \
    enable-weak-ssl-ciphers enable-ssl3 enable-ssl3-method enable-ktls enable-fips \
    "
ARG RESTY_BUILD_OPTS="\
    --with-compat \
    --without-http_rds_json_module \
    --without-http_rds_csv_module \
    --without-lua_rds_parser \
    --without-mail_pop3_module \
    --without-mail_imap_module \
    --without-mail_smtp_module \
    --with-http_addition_module \
    --with-http_auth_request_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_geoip_module=dynamic \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_image_filter_module=dynamic \
    --with-http_mp4_module \
    --with-http_random_index_module \
    --with-http_realip_module \
    --with-http_secure_link_module \
    --with-http_slice_module \
    --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_sub_module \
    --with-http_v2_module \
    --with-http_v3_module \
    --with-http_xslt_module=dynamic \
    --with-mail \
    --with-mail_ssl_module \
    --with-stream \
    --with-stream_ssl_module \
    --with-stream_ssl_preread_module \
    --with-stream_realip_module \
    --with-threads \
    "

COPY openEuler.repo /etc/yum.repos.d/openEuler.repo

RUN set -eux; \
    dnf install -y gcc make autoconf automake libtool git findutils gd-devel libxslt-devel GeoIP-devel && dnf clean all

RUN set -eux; \
    mkdir /openresty-src && \
    curl -O https://openresty.org/download/openresty-${RESTY_VERSION}.tar.gz && \
    tar zxf openresty-${RESTY_VERSION}.tar.gz -C /openresty-src --strip-components=1

RUN set -eux; \
    git clone -b v${ZLIB_VERSION} https://gitcode.com/gh_mirrors/zl/zlib.git /zlib-src && \
    cd /zlib-src && \
    ./configure \
        --prefix=${ZLIB_PREFIX} \
        --libdir=${ZLIB_PREFIX}/lib \
        ${ZLIB_BUILD_OPTS} && \
    make -j$(nproc) && \
    make install

RUN set -eux; \
    git clone -b pcre2-${PCRE2_VERSION} https://gitcode.com/gh_mirrors/pc/pcre2.git /pcre2-src && \
    cd /pcre2-src && \
    autoreconf --install && \
    CFLAGS="-g -O3" ./configure \
        --prefix=${PCRE_PREFIX} \
        --libdir=${PCRE_PREFIX}/lib \
        ${PCRE_BUILD_OPTS} && \
    CFLAGS="-g -O3" make -j$(nproc) && \
    CFLAGS="-g -O3" make install

RUN set -eux; \
    git clone -b openssl-${OPENSSL_VERSION} https://gitcode.com/gh_mirrors/ope/openssl.git /openssl-src && \
    cd /openssl-src && \
    git apply /openresty-src/patches/openssl-${OPENSSL_VERSION}-sess_set_get_cb_yield.patch && \
    ./config \
        --prefix=${OPENSSL_PREFIX} \
        --libdir=${OPENSSL_PREFIX}/lib \
        zlib-dynamic -g \
        -Wl,-rpath,${ZLIB_PREFIX}/lib:${OPENSSL_PREFIX}/lib \
        ${OPENSSL_BUILD_OPTS} && \
    make -j$(nproc) && \
    make install_sw

RUN set -eux; \
    cd /openresty-src && \
    ./configure \
        --prefix=${RESTY_PREFIX} \
        --build="build by Air China IT Group." \
        --with-luajit-xcflags="-DLUAJIT_NUMMODE=2 -DLUAJIT_ENABLE_LUA52COMPAT" \
        --with-pcre \
        --with-pcre-jit \
        --with-cc-opt="-DNGX_LUA_ABORT_AT_PANIC -I${ZLIB_PREFIX}/include -I${PCRE_PREFIX}/include -I${OPENSSL_PREFIX}/include" \
        --with-ld-opt="-L${ZLIB_PREFIX}/lib -L${PCRE_PREFIX}/lib -L${OPENSSL_PREFIX}/lib -Wl,-rpath,${ZLIB_PREFIX}/lib:${PCRE_PREFIX}/lib:${OPENSSL_PREFIX}/lib" \
        ${RESTY_BUILD_OPTS} && \
    make -j$(nproc) && \
    make install && \
    cd /usr/local && \
    find ${RESTY_PREFIX} -name "*.a" -exec rm {} \; && \
    tar zcf /openresty-bin.tar.gz openresty

FROM openeuler-22.03-lts-sp4:latest

COPY gosu-arm64 /usr/local/bin/gosu
COPY openEuler.repo /etc/yum.repos.d/openEuler.repo
COPY --from=build /openresty-bin.tar.gz /

ENV GOSU_VERSION 1.17
RUN set -eux; \
    chmod +x /usr/local/bin/gosu

RUN set -eux; \
    dnf install -y shadow ca-certificates binutils glibc-all-langpacks gd libxslt GeoIP && \
    groupadd -g 70 openresty; \
    useradd -r -u 70 -g openresty -m -d /home/openresty openresty; \
    tar zxf /openresty-bin.tar.gz -C /usr/local; \
    rm -rf /openresty-bin.tar.gz; \
    ln -sf /usr/local/openresty/bin/openresty /usr/local/bin/openresty; \
    mkdir -p /home/openresty/instance; \
    chown -R openresty:openresty /home/openresty/instance; \
    dnf clean all

ENV LANG en_US.UTF-8
EXPOSE 8080 8443
VOLUME ["/home/openresty/instance"]

COPY entrypoint.sh /usr/local/bin/
RUN chmod 755 /usr/local/bin/entrypoint.sh; \
    ln -s /usr/local/bin/entrypoint.sh / # backwards compat

ENTRYPOINT ["entrypoint.sh"]
CMD ["openresty", "-p", "/home/openresty/instance", "-g", "daemon off;"]

STOPSIGNAL SIGQUIT
